#include "MediaLibrary.h"
#include "UAP.pb.h"

namespace tsc
{
namespace common
{

using namespace ProtocolsCommon;
using namespace UserApplicationPart;

const uint32_t MediaLibrary::INVALID_MEDIA_ITEM_ID = 0;

/*******************************************************************************
 *
 * Constructor
 *
 ******************************************************************************/
MediaLibrary::MediaLibrary()
{
	// TODO fill library with data
} // MediaLibrary::MediaLibrary

/*******************************************************************************
 *
 * Destructor
 *
 ******************************************************************************/
MediaLibrary::~MediaLibrary()
{
	for(std::list<PlayableItem*>::iterator it =	mMediaItems.begin();
	    it != mMediaItems.end();
	    ++it)
	{
		delete *it; // delete PlayableItem pointer
		mMediaItems.erase(it); // remove entry from the list
	}

}

MediaLibrary& MediaLibrary::instance()
{
	static MediaLibrary ml;
	return ml;
}

std::list<PlayableItem*>*
MediaLibrary::getAvailableMedia(ClientCapabilities* capabilities)
{
	std::list<PlayableItem*>* newList = new std::list<PlayableItem*>();

	for(std::list<PlayableItem*>::iterator it = mMediaItems.begin();
		it != mMediaItems.end();
		++it)
	{
		if(isMatching(*it, capabilities))
		{
			newList->push_back(*it);
		}
	}

	return newList;
}

bool
MediaLibrary::isMatching(
	PlayableItem* item,
	UserApplicationPart::ClientCapabilities* cap)
{
	// for now just return all audio
	if(item->type() == PlayableItem::TYPE_AUDIO)
	{
		return true;
	}
	return false;
}

bool
MediaLibrary::itemExists(uint32_t mediaItemId)
{
    for(std::list<PlayableItem*>::iterator it = mMediaItems.begin();
        it != mMediaItems.end();
        ++it)
    {
        if((*it)->id() == mediaItemId)
        {
            return true;
        }
    }
    return false;
}

} // namespace common
} // namespace tsc
